#include "dxgamegraphic.h"
#include "logger.h"

using namespace std;

DXGameGraphic* DXGameGraphic::pInstance = 0;

DXGameGraphic::DXGameGraphic()
{ }

DXGameGraphic::~DXGameGraphic()
{	
    if (pD3DDevice != NULL)
        pD3DDevice->Release();
    if (pD3D != NULL)
        pD3D->Release();
}

DXGameGraphic* DXGameGraphic::GetInstance()
{
    if (pInstance == 0)
    {
        pInstance = new DXGameGraphic();
    }
    return pInstance;
}

void DXGameGraphic::SetProperties(HWND _hWnd, int w, int h, bool _fullScreen)
{	
    pInstance->hWnd = _hWnd;
    pInstance->pD3D = NULL;
    pInstance->pD3DDevice = NULL;
    pInstance->widthScreen = w;
    pInstance->heightScreen = h;
    pInstance->fullScreen = _fullScreen;
}

void DXGameGraphic::Destroy()
{
    if (pInstance != 0)
    {   
        delete pInstance;
    }
    return;  
}

bool DXGameGraphic::Init()
{	
    //Init DirectX graphic
    pD3D = Direct3DCreate9(D3D_SDK_VERSION);
    if (pD3D == NULL)
    {	
        (Logger::GetLogger("DXGameGraphic")).Error("Can't create Direct3D!");
        return false;
    }

    d3dpp.BackBufferWidth = widthScreen;
    d3dpp.BackBufferHeight = heightScreen;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferCount = 1;
    d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
    d3dpp.MultiSampleQuality = 0;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.Windowed = !fullScreen;
    d3dpp.EnableAutoDepthStencil = 0;
    d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
    d3dpp.Flags = 0;
    d3dpp.FullScreen_RefreshRateInHz = 0;
    d3dpp.PresentationInterval = 0;

    // Create Direct3D Device
    if FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,
                        D3DDEVTYPE_HAL,
                        hWnd, 
                        D3DCREATE_HARDWARE_VERTEXPROCESSING, 
                        &d3dpp, 
                        &pD3DDevice))
        if FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,
                        D3DDEVTYPE_HAL,
                        hWnd, 
                        D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
                        &d3dpp, 
                        &pD3DDevice))
            if FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,
                        D3DDEVTYPE_REF,
                        hWnd, 
                        D3DCREATE_HARDWARE_VERTEXPROCESSING, 
                        &d3dpp, 
                        &pD3DDevice))
            {	
                (Logger::GetLogger("DXGameGraphic")).Error("Can't create Direct3D Device!");
                return false;
            }
    return true;
}

void* DXGameGraphic::GetDevice()
{	
    return pD3DDevice;
}

int DXGameGraphic::GetWidthScreen()
{
    return widthScreen;
}

int DXGameGraphic::GetHeightScreen()
{	
    return heightScreen;
}

bool DXGameGraphic::BeginRender()
{	
    if (pD3DDevice == NULL)
        return false;
    pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    if FAILED(pD3DDevice->BeginScene())
        return false;
    return true;
}

bool DXGameGraphic::EndRender()
{	
    if (pD3DDevice == NULL)
        return false;
    if FAILED(pD3DDevice->EndScene())
        return false;
    if FAILED(pD3DDevice->Present(NULL, NULL, NULL, NULL))
        return false;
    return true;
}

bool DXGameGraphic::IsFullScreen()
{	
    return fullScreen;
}
